package com.eagle.concurrency.example.atomic;

import com.eagle.concurrency.annoations.ThreadSafe;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

@ThreadSafe
public class AtomicIntDemo {
	private static AtomicInteger count = new AtomicInteger();
	private static ExecutorService executorService = Executors.newCachedThreadPool();
	private static Semaphore semaphore = new Semaphore(200);

	public static void main(String[] args) throws Exception{

		for (int i = 0; i < 5000; i++) {
			executorService.execute(()->{
				try {
					semaphore.acquire();
					add();
					semaphore.release();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
		executorService.shutdown();
		System.out.println(count.get());
	}

	private static void add(){
		count.getAndIncrement();
	}
}
